import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "firstFilter")
public class firstFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {

	HttpSession session= req.getSession(true);
	String reqstr=req.getRequestURL().toString();
	System.out.println(reqstr);
	if(session.isNew())
	{
	    res.sendRedirect("index.html");
	}else if (reqstr.equals("http://localhost:8080/login/dgut"))
	{
	    if(req.getParameter("token") == null)
		res.sendRedirect("https://cas.dgut.edu.cn?appid=javaee");
	}else if((reqstr.equals("http://localhost:8080/index.html") || reqstr.equals("http://localhost:8080"))&& req.getSession().getAttribute("Login")!=null && req.getSession().isNew() == false)
		res.sendRedirect("profile.jsp");

	chain.doFilter(req,res);
    }
}
